﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LAVAA.BL.Control;
using LAVAA.DAL;
using LAVAA.WEB.Models;

namespace LAVAA.WEB.Controllers
{
    public class UserGroupController : Controller
    {
        //
        // GET: /UserGroup/
        public ActionResult Index()
        {
            return View(new LAVAA.BL.Control.UserGroupFacade().GetAllUserGroups());
        }

        //
        // GET: /UserGroup/Create

        public ActionResult Create()
        {
            if (Request.IsAjaxRequest())
                return PartialView("_Create");
            return View();
        }

        //
        // POST: /UserGroup/Create

        [HttpPost]
        public ActionResult Create(UserGroup usergroup)
        {
            UserGroupFacade userGroupFacade = new UserGroupFacade();
            if (ModelState.IsValid)
            {
                try
                {
                    userGroupFacade.AddUserGroup(usergroup);
                    if (Request.IsAjaxRequest())
                        return Json(new { success = true });
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError(string.Empty, e.Message);
                }
            }
            if (Request.IsAjaxRequest())
                return PartialView("_Create", usergroup);
            return View(usergroup);
        }

        //
        // GET: /UserGroup/Edit/5

        public ActionResult Edit(Guid id)
        {
            UserGroupFacade userGroupFacade = new UserGroupFacade();
            UserGroup usergroup = userGroupFacade.FindUserGroupById(id);
            if (usergroup == null)
            {
                return HttpNotFound();
            }
            return View(usergroup);
        }

        //
        // POST: /UserGroup/Edit/5

        [HttpPost]
        public ActionResult Edit(UserGroup usergroup)
        {
            UserGroupFacade userGroupFacade = new UserGroupFacade();
            if (ModelState.IsValid)
            {
                userGroupFacade.UpdateUserGroup(usergroup);
                return RedirectToAction("Index");
            }
            return View(usergroup);
        }

        //
        // GET: /UserGroup/Delete/5

        public ActionResult Delete(Guid id)
        {
            UserGroupFacade userGroupFacade = new UserGroupFacade();
            try
            {
                userGroupFacade.DeleteUserGroup(id);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
                return RedirectToAction("Index");
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}